/**
 * 
 */
package de.mbenning.fsim.web.utils;

import java.beans.PropertyEditorSupport;

import de.mbenning.fsim.domain.Kontinent;
import de.mbenning.fsim.persistence.dao.interfaces.IMainDAO;

/**
 * @author Martin.Benning
 *
 */
public class KontinentPropertyEditorSupport extends PropertyEditorSupport {
	
	private IMainDAO mainDAO;
	
	public KontinentPropertyEditorSupport(IMainDAO mainDAO) {
		this.mainDAO = mainDAO;
	}

	@Override
	public String getAsText() {
		Kontinent kontinent = (Kontinent) getValue();
		if(kontinent != null) {
			return kontinent.getId().toString();
		}
		return null;
	}

	@Override
	public void setAsText(String text) throws IllegalArgumentException {
		if(text == null || text.equalsIgnoreCase("")) {
			setValue(null);
			return;
		}
		Integer id = null;
        try {
            id = Integer.parseInt(text);
        } catch (NumberFormatException e) {
            throw new IllegalArgumentException(e);
        }
        try {
        	Kontinent kontinent = mainDAO.getKontinentById(id);
        	setValue(kontinent);
        } catch (Exception e) {
            throw new IllegalArgumentException(e);
        }
	}

}
